package com.tywj.panda.home.notice

import android.app.Activity
import android.os.Bundle
import com.alibaba.android.arouter.facade.annotation.Route
import com.tywj.lib.core.common.util.BarUtil
import com.tywj.lib.core.common.util.ResourcesUtil
import com.tywj.lib.route.RouteParam
import com.tywj.panda.customer.common.http.PDStringCallback
import com.tywj.panda.customer.common.tool.TimeTool
import com.tywj.panda.customer.common.view.base.MyBaseActivity
import com.tywj.panda.home.R
import com.tywj.panda.home.api.HomeApi
import com.tywj.panda.home.entity.NoticeEntity
import com.tywj.panda.home.event.HomeEvent
import com.tywj.panda.home.route.HomeRoute
import kotlinx.android.synthetic.main.home_activity_notice_details.*

/**
 * @desc 消息通知详情界面
 * @author June Yang
 * @date 2020/5/27
 */
@Route(path = HomeRoute.NOTICE_DETAILS)
class NoticeDetailsActivity : MyBaseActivity() {

    companion object {
        const val PARAMS_DETAILS = "noticeDetails"
        const val PARAMS_IS_FIRST_NOTICE = "first_notice"
    }

    private lateinit var noticeDetailsBean: NoticeEntity.DataBean
    private var isFirstNotice = false

    override fun getLayoutID(): Int = R.layout.home_activity_notice_details


    override fun initVariable(savedInstanceState: Bundle?) {
        super.initVariable(savedInstanceState)

        val routeParam = RouteParam(this)
        noticeDetailsBean = routeParam.getParcelable(PARAMS_DETAILS)!!
        isFirstNotice = routeParam.getBoolean(PARAMS_IS_FIRST_NOTICE)

    }

    override fun initView(savedInstanceState: Bundle?) {
        super.initView(savedInstanceState)
        BarUtil.setStatusBarColor(this, ResourcesUtil.getColor(R.color.common_white))
        tvNoticeTitle.text = noticeDetailsBean.title
        tvTime.text = TimeTool.formatTime1(noticeDetailsBean.createDate)
        tvNoticeDetails.text = noticeDetailsBean.content
    }

    override fun loadDataOnCreate() {
        super.loadDataOnCreate()
        updateReadStatus()
    }

    private fun updateReadStatus() {
        //已读
        if (noticeDetailsBean.read == 1) {
            return
        }
        HomeApi.readNotice(noticeDetailsBean.id).attachToLifecycle(this)
            .execute(object : PDStringCallback() {
                override fun success(message: String) {
                    setResult(Activity.RESULT_OK)

                    if (isFirstNotice) {
                        HomeEvent.refreshNoticeEvent.post(Unit)
                    }
                }
            })
    }
}

